package com.muyan.design.prototype;

import java.util.List;

import com.google.common.collect.Lists;

public class Test {

    public interface Observer {
        public void accept();
    }

    public class MyObserver implements Observer {
        @Override
        public void accept() {
            System.out.println("MyObserver accepted the msg...");
        }
    }

    public class YourObserver implements Observer {

        @Override
        public void accept() {
            System.out.println("YourObserver accepted the msg...");
        }
    }

    public interface Subject {
        void addObserver(Observer o);

        void delObserver(Observer o);

        void inform();

        void doInform();
    }

    public abstract class AbstractSubject implements Subject {
        private List<Observer> lists = Lists.newArrayList();

        @Override
        public void addObserver(Observer o) {
            lists.add(o);
        }

        @Override
        public void delObserver(Observer o) {
            lists.remove(o);
        }

        @Override
        public void doInform() {
            for (Observer o : lists) {
                o.accept();
            }
        }
    }

    public class MySubject extends AbstractSubject {
        @Override
        public void inform() {
            doInform();
        }
    }


    public static void main(String[] args) {
        Test t = new Test();
        Subject subject = t.new MySubject();
        subject.addObserver(t.new MyObserver());
        subject.addObserver(t.new YourObserver());
        subject.inform();
    }
}
